/**
 * @(#)FileUploadServiceImpl.java 2017年5月3日
 * Copyright(C) 2017年5月3日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;



import org.jsbd.boss.domian.channel.FileUploadVo;
import org.jsbd.boss.service.IFileUploadService;
import org.jsbd.boss.util.OssUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import edu.hziee.cap.common.util.Md5Utils;

/**
 * TODO 请加入本类的说明
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */

@Service("fileUploadService")
public class FileUploadServiceImpl implements IFileUploadService {
	
	public static final Logger LOGGER = LoggerFactory.getLogger(FileUploadServiceImpl.class);

	@Override
	public FileUploadVo uploadFile(byte[] fileData,String bucketName,String objectName,String fileName,String fileMd5) {
		FileUploadVo fileVo = null;
		if (fileData != null &&fileData.length>0 ) {
			try {
				fileVo = new FileUploadVo();
				String md5 = Md5Utils.md5Hex(fileData);
				if(!md5.equals(fileMd5)){
					LOGGER.warn("md5 is not save and file [{}] is lacking",objectName);
					return null;
				}
				
				long startTime=System.currentTimeMillis();
				OssUtils.uploadFile(bucketName,objectName, fileData);
				LOGGER.warn("上传[{}],耗时[{}]",objectName,(System.currentTimeMillis()-startTime));
				
				fileVo.setMd5(md5);
				fileVo.setFilePath(objectName);
			} catch (Exception e) {
				LOGGER.error("uploadFile error", e);
			}
		}
		return fileVo;
	}
	
}
